package com.wpam.scrabble.mobile.ui;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;

import com.wpam.scrabble.mobile.GameResources;
import com.wpam.scrabble.mobile.R;

public class LettersStand extends Drawable{
	
	private LettersStandSquare[] letterSlots_;
	private Bitmap background_;
	private int startHeight_;
	private int height_;
	private int tileSize_;
	private Paint bitmapPaint_;
	
	public LettersStand() {
		
		Resources resources = GameResources.getInstance().getResources();
		this.background_ = BitmapFactory.decodeResource(resources, R.drawable.letter_stand);
		this.height_ = Math.min((int) (resources.getDisplayMetrics().heightPixels * 0.12), 45);
		this.tileSize_ = Math.min(SquareBitmap.DEFAULT_SIZE, (int) (height_ * 0.8));
		this.background_ = Bitmap.createScaledBitmap(background_, resources.getDisplayMetrics().widthPixels, height_, true);
		startHeight_ = resources.getDisplayMetrics().heightPixels - height_;
		int offsetX = (int) (tileSize_ * 0.125);
		int offsetY = (int) (height_ - tileSize_) / 2;
		int beginning = (int) ((resources.getDisplayMetrics().widthPixels - 7 * tileSize_ - 6 * offsetX) / 2.0);
		this.letterSlots_ = new LettersStandSquare[7];
		for (int i = 0; i < letterSlots_.length; ++i)
			this.letterSlots_[i] = new LettersStandSquare(beginning + (tileSize_ + offsetX) * i, startHeight_ + offsetY, tileSize_);
		
		this.bitmapPaint_ = new Paint();
	}

	public void resize() {

		Resources resources = GameResources.getInstance().getResources();
		int h = resources.getDisplayMetrics().heightPixels;
		System.out.print(h);
		this.background_ = BitmapFactory.decodeResource(resources, R.drawable.letter_stand);
		this.height_ = Math.min((int) (resources.getDisplayMetrics().heightPixels * 0.12), 45);
		this.tileSize_ = Math.min(SquareBitmap.DEFAULT_SIZE, (int) (height_ * 0.8));
		this.background_ = Bitmap.createScaledBitmap(background_, resources.getDisplayMetrics().widthPixels, height_, true);
		startHeight_ = resources.getDisplayMetrics().heightPixels - height_;
		int offsetX = (int) (height_ * 0.125);
		int offsetY = (int) (height_ - tileSize_) / 2;
		int beginning = (int) ((resources.getDisplayMetrics().widthPixels - 7 * tileSize_ - 6 * offsetX) / 2.0);
		for(int i = 0; i < letterSlots_.length; ++i)
			this.letterSlots_[i].onResize(beginning + (tileSize_ + offsetX) * i, startHeight_ + offsetY, tileSize_);
	}
	
	public LettersStandSquare getSquare(int x, int y) {
		
		if (y < startHeight_)
			return null;
		
		for (LettersStandSquare square : letterSlots_) {
			if (square.amIHit(x, y))
				return square.pickUp();
		}
		return null;
	}
	
	public LettersStandSquare[] getLetters() {
		return letterSlots_;
	}
	
	public int getAmountOfMissingLetters() {
		
		int num = 0;
		for(LettersStandSquare square : letterSlots_) {
			if (square.letter_.equals(""))
				++num;
		}
		return num;
	}
	
	public void setLetters(String letters) {

		int pos = 0;
		for (LettersStandSquare square : letterSlots_) {
			if (pos >= letters.length())
				break;
			if (square.getLetter().equals("")) {
				square.setLetter(String.valueOf(letters.charAt(pos)));
				square.setUsable(true);
				++pos;
			}
		}
	}

	public void reset() {
		
		for (LettersStandSquare square : letterSlots_)
			square.reset();
	}
	
	@Override
	public void draw(Canvas canvas) {
		
		canvas.drawBitmap(background_, 0, startHeight_, bitmapPaint_);
		LettersStandSquare onTop = null;
		for (LettersStandSquare square : letterSlots_) {
			if (!square.isPickedUp())
				square.draw(canvas);
			else
				onTop = square;
		}
		if (onTop != null)
			onTop.draw(canvas);
	}

	@Override
	public int getOpacity() {
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {
		
		bitmapPaint_.setAlpha(alpha);
		for (LettersStandSquare square : letterSlots_)
			square.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {

		bitmapPaint_.setColorFilter(cf);
		for (LettersStandSquare square : letterSlots_)
			square.setColorFilter(cf);
	}

	public void takeAwayLetters(String letters) {
		
		String currentLetter;
		mainLoop: for (int i = 0; i <letters.length(); ++i) {
			currentLetter = String.valueOf(letters.charAt(i));
			for (LettersStandSquare square : letterSlots_) {
				if (square.getLetter().equals(currentLetter)) {
					square.putDown(true);
					continue mainLoop;
				}
			}
		}
	}

	public int getHeight() {
		return height_;
	}

}
